package com.springapp.utils;


import java.net.UnknownHostException;

import com.mongodb.*;
import com.mongodb.client.MongoDatabase;

public class MongoManager {
    private final static String HOST = "localhost";// 端口
    private final static int PORT = 27017;// 端口
    private final static int POOLSIZE = 100;// 连接数量
    private final static int BLOCKSIZE = 100; // 等待队列长度
    private static MongoClient mongo = null;

    private MongoManager() { }

    static {
        initDBPrompties();
    }

    public static MongoDatabase getDB(String dbName) {
        return mongo.getDatabase(dbName);
    }

    /**
     * 初始化连接池
     */
    private static void initDBPrompties() {
        // 其他参数根据实际情况进行添加
        try {
            mongo = new MongoClient(HOST, PORT);
            MongoClientOptions opt = mongo.getMongoClientOptions();
            //opt.connectionsPerHost = POOLSIZE;
            //opt.threadsAllowedToBlockForConnectionMultiplier = BLOCKSIZE;
        } /*catch (UnknownHostException e) {
            e.printStackTrace();
        }*/ catch (MongoException e) {
            e.printStackTrace();
        }

    }
}
